/*
 * 退菜
 * 2018-03-23
 */
;(function ($) {
	//退菜入口
	$.fn.posorderRetreat=function(options){
    	//创建对象
		var $ele = new retreat(this, options);
		//入口
		$ele.getDefault();
	}
	
	//构造函数
    var retreat = function(ele, opt) {   	
    	//选择器对象    	
        this.$element=ele;   
        //默认参数
        this.defaults = {
        	no:"",//no
    		xh:"",//序号
    		reason:"",//退还原因
    		sl:"",//数量
    		printchecked:"",//出单
    		getDefaultBack:function(){},//获取默认值完毕回调函数
        	workComplete:function(){},//插件加载完成
    		dataBack:function(){},//完成结账回调函数
    		returnBack:function(){},//返回回调函数
        },       
        //初始化参数
        this.options = $.extend({}, this.defaults, opt);
    }

    //属性方法
    retreat.prototype = {
    	//临时变量
		defaultValue:null,
    	//获取默认值
    	getDefault:function(arg){
    		var $t=this;
    		var $opt=$t.options;
    		
    		var postData={};
    		if(!arg){
    			arg = {};
    		}
    		postData.arg={"no":$opt.no,"xh":$opt.xh,"sl":$opt.sl,"reason":$opt.reason,"chk_printchecked":$opt.printchecked};
    		postData.arg = $.extend(postData.arg,arg);
    		$.ajax({
    			url: $.HSAPI_AJAXURL+"/qms/mobile/procedure2",
    			type: "post",
    			dataType: "json",
    			data: $.HSAPI_ASKJSONINFO(JSON.stringify(postData),'POSOrderSendBack',"{}"),
    		}).success(function (data) {
    			if(data.RETURN==1){
    				$opt.getDefaultBack(data);
    			}else if(data.RETURN==-1){
    				jf_alert(data.MSG);
					$opt.returnBack();
    			}else if(data.RETURN==-10){
    				$t.errorContent(arg,data);
    			}
				
    		}).error(function () {
    			jf_alert(JSON.stringify(data));
    			$opt.returnBack();
    		}).complete(function () {
    		});
		},
		errorContent:function(arg,data){
			var $t=this;
    		var $opt=$t.options;
			/*------------------------------------------------*/
			
			var MSG = data.MSG;
			var name = data.checkbox;
			var value = (data.checkboxvalue||"");
			
			var checked = "";
			if(value==1){
				checked = "checked"
			}
			
			var html = 	'<h3 class="header smaller lighter blue">'+
							'<i class="ace-icon fa fa-bullhorn"></i>'+MSG+
						'</h3>'+
						'<div class="alert alert-block alert-danger">'+
							'<label>'+
								'<input id="checkin_cccheckbox" type="checkbox" name="'+name+'" value="" class="ace" '+checked+'/>'+
								'<span class="lbl" style="color:#478fca;">'+MSG+'</span>'+
							'</label>'+
						'</div>';

			bootbox.dialog({
				message: html,
				buttons:{
					"button1":{
						"label" : $.i18n.propJsp("确定"),
						"className" : "btn-sm btn-info",
						"callback" : function(){
							if($("#checkin_cccheckbox").prop("checked")){
								arg[name]="yes";
								$t.getDefault(arg);
							}else{
								arg[name]="no";
								$t.getDefault(arg);
							}
						}
					},	
					"button" :
					{
						"label" : $.i18n.propJsp("确定"),
						"className" : "btn-sm",
					},
				}
			});
		},
		//返回筛选器对象,用于链式操作
    	hsOle: function() {
            return this.$element;      
        }
    }
})(jQuery);